/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shogi;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;

/**
 *
 * @author andy
 */
public class Options{
        JMenuBar mb;
        JMenu game;
        JMenu options;
        JFrame frame;
        JMenu resource;
        JMenu file;
        JMenuItem help;
        JMenuItem version;
        JMenuItem credits;
        JMenuItem save;
        JMenuItem resume;
        JMenuItem pause;
        JMenuItem quit;
        JMenu shogiPiece;
        JMenuItem newGame;
        JMenuItem load;
        JRadioButtonMenuItem kanji, western;
        String loadGame;
        String reset = "lxpxxxPxLnbpxxxPRNsxpxxxPxSgxpxxxPxGkxpxxxPxKgxpxxxPxGsxpxxxPxSnrpxxxPBNlxpxxxPxL000000000000001";
        String directory = "resources/";
        
        Options(JFrame shogiframe)
        {        
                frame = shogiframe;                
                setBar();             
                addListeners();
        }
        void setBar()
        {
                mb = new JMenuBar();
                file = new JMenu("File");
                game = new JMenu("Game");
                resource = new JMenu("Resource");
                frame.setJMenuBar(mb);
                mb.add(file);
                mb.add(game);
                mb.add(resource);
                setFile();
                setResource();
                setGame();
        }
        void setFile()
        {
                save = new JMenuItem("Save");               
                quit = new JMenuItem("Exit");
                load = new JMenuItem("Load");
                file.add(save); file.add(load); file.addSeparator(); file.add(quit);
        }
        void setGame()
        {
                shogiPiece = new JMenu("Shogi Piece");
                newGame = new JMenuItem("New Game");
                resume = new JMenuItem("Resume");
               pause = new JMenuItem("Pause", KeyEvent.VK_O);
               kanji = new JRadioButtonMenuItem("Kanji", new ImageIcon(getClass().getResource("resources1/kLeft.png")));
               western = new JRadioButtonMenuItem("Western",  new ImageIcon(getClass().getResource("resources/kLeft.png")));
               ButtonGroup bg = new ButtonGroup();
               bg.add(western);
               western.isSelected();
               bg.add(kanji);
               western.setSelected(true);
               shogiPiece.add(western);
               shogiPiece.add(kanji);
                game.add(shogiPiece);
                game.addSeparator();
                game.add(pause);
                game.addSeparator();
                game.add(newGame);
        }
        void setResource()
        {
                help = new JMenuItem("help");
                version = new JMenuItem("Version");
                credits = new JMenuItem("Credits");
                resource.add(help);
                resource.add(version);
                resource.add(credits);
        }
       ActionListener action = new ActionListener()
       {
                public void actionPerformed(ActionEvent e)
                {
                         AbstractButton rb = (AbstractButton) e.getSource();
                        if(rb == version)
                        {
                                 String s = "shogi version 1.0";
                                 JOptionPane.showMessageDialog(null, s);
                        }
                        if(rb == credits)
                        {
                                String s = "Team Members:"
                                        + "\nNova"
                                        + "\nCody"
                                        + "\nAndy";
                                JOptionPane.showMessageDialog(null , s);
                        }
                        if(rb == quit)
                        {
                                exit();
                        }
                        if(rb == pause)
                        {
                                JOptionPane.showMessageDialog(null, "COFFEE BREAK!!");
                        }
                        if(rb == kanji)
                        {
                                directory = "resources1/";
                                 char[] s = ((ShogiBoard)frame).getSaveGame();
                               String saved = new String(s);
                                directory = "resources1/";
                                frame.dispose();
                                 new Shogi(saved, directory);
                        }
                        if(rb == western)
                        {
                                directory = "resources/";
                                char[] s = ((ShogiBoard)frame).getSaveGame();
                               String saved = new String(s);
                                directory = "resources/";
                                frame.dispose();
                                new Shogi(saved, directory);
                        } 
                        if(rb == save)
                        {
                               JFileChooser fc = new JFileChooser();
                               char[] s = ((ShogiBoard)frame).getSaveGame();
                               String saved = new String(s);
                               String d = ShogiBoard.getIconSetDirectory();
                               int returnVal = fc.showSaveDialog(save);
				if (returnVal == JFileChooser.APPROVE_OPTION) 
				{            
                                       File file = fc.getSelectedFile();
                                       try{
                                        BufferedWriter out = new BufferedWriter(new FileWriter(file));
                                        out.write(saved);
                                        out.write("\n"+d);
                                        out.close();
                                       }catch(Exception x){JOptionPane.showMessageDialog(null , "error saving!");}
				}            
                        }
                        if(rb == load)
                        {
                                JFileChooser fc = new JFileChooser();
                                int returnVal = fc.showOpenDialog(load);
				if (returnVal == JFileChooser.APPROVE_OPTION) 
				{            
                                       File file = fc.getSelectedFile();
                                       try{                                       
                                        BufferedReader in = new BufferedReader(new FileReader(file.getAbsolutePath()));
                                        loadGame = in.readLine();
                                        directory = in.readLine();
                                        in.close();
                                        frame.dispose();
                                        new Shogi(loadGame, directory);
                                        //((ShogiBoard)frame).loadBoard(loadGame);
                                       }catch(Exception x){JOptionPane.showMessageDialog(null , "error loading!");}
				} 
                        }
                        if(rb == newGame)
                        {
                                 frame.dispose();
                                 new Shogi(reset, directory);
                               // ((ShogiBoard)frame).loadBoard(reset);
                        }
                        if(rb == help)
                        {
                                //String wd = getWorkingDir()+;
                                try 
                                {
                                        //if ((new File(wd)).exists()) 
                                        //{ 
                                                Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+getClass().getResource("Shogi.pdf"));
                                                p.waitFor();
                                       /* } else {
                                                System.out.println("File is not exists");
                                        }*/
                                        System.out.println("Done");
                                } catch (Exception ex) {
                                        ex.printStackTrace();
                                 }
                        }
                }
       };
        void addListeners()
        {
               version.addActionListener(action);
               credits.addActionListener(action);
               quit.addActionListener(action);
               pause.addActionListener(action);
               western.addActionListener(action);
               kanji.addActionListener(action);
                save.addActionListener(action);
                load.addActionListener(action);
                newGame.addActionListener(action);
                help.addActionListener(action);
        }
        void exit()
         {
                System.exit(0);
        }
        String getWorkingDir()
        {
                File dir1 = new File (".");
                 File dir2 = new File ("..");
                 String current = null, parent = null;
                try {
                 current = dir1.getCanonicalPath();
                 parent = dir2.getCanonicalPath();
                 }
                 catch(Exception e) {
                         e.printStackTrace();
                }
                return current;
        }
}
